// pages/more/more.js
// import Dialog from '@vant/weapp/dialog/dialog';
// const util = require('../../utils/util')
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
import Toast from '@vant/weapp/toast/toast';
import util from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: wx.getStorageSync('user').userName || '点击设置昵称',
    userInfo: {},
    avatarUrl: wx.getStorageSync('user').img || defaultAvatarUrl,
    userID: '',
    mychatList: [],
  },
  toChat(e) {
    console.log(e.currentTarget.dataset.id)
    let data = JSON.stringify(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: `../../subpkg/chatwx/index?data=${encodeURIComponent(data)}`,
    })
  },
  onChooseAvatar(e) {
    console.log('e', e)
    let opid = wx.getStorageSync('openid')
    const { avatarUrl } = e.detail
    this.setData({
      avatarUrl
    })
    wx.cloud.uploadFile({
      cloudPath: `userImg/${opid}.png`,
      filePath: avatarUrl
    }).then(res => {
      //返回该图片文件路径fileID
      wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'setImgUrl',
          img: res.fileID,
          openid: opid,
        }
      }).then((resp) => {
        Toast.success('头像上传成功');
      }).catch((e) => {console.log('e', e)});
    }).catch(error => {console.log(error)})
  },
  bindblurFn(e) {
    console.log("bindblurFn", e.detail.value)
    this.setData({userName: e.detail.value})
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'setUserName',
        userName: this.data.userName,
        openid: wx.getStorageSync('openid'),
      }
    }).then((resp) => {
      Toast.success('昵称上传成功');
    }).catch((e) => {console.log('e', e)});
  },

  toUserName() {
    wx.navigateTo({
      url: '../../subpkg/setName/index',
    })
  },
  toSell(e) {
    wx.navigateTo({
      url: `../../subpkg/sellOrbuy/index?type=${e.currentTarget.dataset.type}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '我的',
    })
    // this.setData({
    //   userID: wx.getStorageSync('user')._id.slice(24,32)
    // })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(wx.getStorageSync('userName'))
    // this.setData({
    //   userName: wx.getStorageSync('userName') || '点击设置昵称',
    // })
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'getUser',
        openid: wx.getStorageSync('openid'),
      }
    }).then((resp) => {
        // console.log('resp', resp)
        wx.setStorageSync('user', resp.result.data[0])
        this.setData({
          userInfo: resp.result.data[0],
        })
    }).catch((e) => {console.log('e', e)});
    // 聊天
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'getMyMsg',
        sendId: wx.getStorageSync('openid'),
      }
    }).then((resp) => {
      console.log('getMyMsg', resp)
      console.log('listlistlist', list)
        let list = resp.result.list.reverse()
        let myId = wx.getStorageSync('openid')

        let chatIdList = []
        let chatList = []
        list.forEach(item => {
          item.creatTime = item.creatTime.slice(6,16)
          if (item.sendId == myId) {
            if (!chatIdList.includes(item.getId)) {
              chatIdList.push(item.getId)
              chatList.push(item)
            }
          } else {
            if (!chatIdList.includes(item.sendId)) {
              chatIdList.push(item.sendId)
              chatList.push(item)
            }
          }
        })
        console.log('chatIdList', chatIdList)
        this.setData({mychatList:chatList})
        console.log('mychatList', this.data.mychatList)
    }).catch((e) => {console.log('e', e)});
    // 忙鸟号
    this.setData({
      userID: wx.getStorageSync('user')._id.slice(24,32)
    })
  },
  toRz() {
    wx.navigateTo({
      url: '../../subpkg/rz/index',
    })
  },
  toRz2() {
    if (wx.getStorageSync('openid') == 'oJ_O35X6Ygur6syGG_CHEeef1ETo') {
      wx.navigateTo({
        url: '../../subpkg/rzAudit/index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(ops) {

  },
})